/**
 * 初始化部门详情对话框
 */
var PayServiceInfoDlg = {
    payServiceInfoData: {},
    zTreeInstance: null,
    validateFields: {
        department: {
            validators: {
                notEmpty: {
                    message: '科室名称不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '科室名称长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        firstLevelProject: {
            validators: {
                notEmpty: {
                    message: '一级项目不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '一级项目长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        payServiceName: {
            validators: {
                notEmpty: {
                    message: '产品项目名称不能为空'
                },
                stringLength: {
                    max: 70,
                    message: '产品项目名称长度不能超过70，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        payServicePriceOne: {
            validators: {
                notEmpty: {
                    message: '产品价格1不能为空'
                },
            }
        },
        payServicePriceTwo: {
            validators: {
                notEmpty: {
                    message: '产品价格2不能为空'
                },
            }
        },
    }
};

/**
 * 清除数据
 */
PayServiceInfoDlg.clearData = function () {
    this.payServiceInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PayServiceInfoDlg.set = function (key, val) {
    this.payServiceInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PayServiceInfoDlg.get = function (key) {
    return $("#" + key).val();
}


/**
 * 关闭此对话框
 */
PayServiceInfoDlg.close = function () {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
PayServiceInfoDlg.onClickPayService = function (e, treeId, treeNode) {
    $("#pName").attr("value", PayServiceInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
}
/**
 * 显示部门选择的树
 *
 * @returns
 */
PayServiceInfoDlg.showPayServiceSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentPayServiceMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
PayServiceInfoDlg.hidePayServiceSelectTree = function () {
    $("#parentPayServiceMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
PayServiceInfoDlg.collectData = function () {
    this.set('id').set('payService').set('updateMoney').set('totalAmount').set('referraler')
        .set('payServiceNum').set('department').set('firstLevelProject').set('payServiceId')
        .set('unit').set('remarks').set('customerId').set('zheMoney').set('num').set('payServiceName').set('payServicePriceOne')
        .set('payServicePriceTwo').set('leftUp').set('rightUp').set('leftDown').set('rightDown').set('doctor').set('nurse');
};

/**
 * 验证数据是否为空
 */
PayServiceInfoDlg.validate = function () {
    $('#payServiceInfoForm').data("bootstrapValidator").resetForm();
    $('#payServiceInfoForm').bootstrapValidator('validate');
    return $("#payServiceInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
PayServiceInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var payServicePriceOne = $('#payServicePriceOne').val();
    var payServicePriceTwo = $('#payServicePriceTwo').val();
    if (payServicePriceOne != "" && payServicePriceOne != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceOne))) {
            alert("产品价格1整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    if (payServicePriceTwo != "" && payServicePriceTwo != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceTwo))) {
            alert("产品价格2整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/payService/addPayService", function (data) {
        Feng.success("添加成功!");
        window.parent.PayService.table.refresh();
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};

PayServiceInfoDlg.updateMoney = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var updateMoney = $('#updateMoney').val();
    var zheMoney = $("#zheMoney").val();
    if (updateMoney != "" && updateMoney == "") {
        alert("请输入修改金额！");
        return
    }
    if (parseFloat(updateMoney) > parseFloat(zheMoney)) {
        alert("修改金额不能大于折后金额，请重新输入！...");
        return
    }
    var ajax = new $ax(Feng.ctxPath + "/payService/updateMoney", function (data) {
        Feng.success("修改成功!");
        var txt = '<ul>';
        var totleReceivable = 0.00;
        var totleFoldingPay = 0.00;
        var customerId = window.parent.document.getElementById("customerId").value;
        if (data.length == 0) {
            window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
            window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
        } else {
            for (var i = 0; i < data.length; i++) {
                var pnum = data[i].payServiceNum;
                var psptwo = data[i].payServicePriceTwo;
                var foldingPay = data[i].foldingPay;
                totleReceivable += pnum * psptwo.toFixed(2);
                totleFoldingPay += foldingPay;
                txt += ' <div class="data_row" style="margin-top: 5px">' +
                    '                <div style=\"padding-left: 10px;width: 120px;display: inline-block;position: relative;top: 11px;\">\n' +
                    '                    <div style=\"border-bottom: 1px solid;\">\n' +
                    '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_top\">' + data[i].leftUp + '</div><!--\n' +
                    '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightUp + '</div>\n' +
                    '                   </div>\n' +
                    '                    <div>\n' +
                    '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_bottom\">' + data[i].leftDown + '</div><!--\n' +
                    '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightDown + '</div>\n' +
                    '                   </div>\n' +
                    '                </div>\n' +
                    ' ' + data[i].department + '&nbsp;&nbsp;' + data[i].firstLevelProject + '&nbsp;&nbsp;' + data[i].payServiceName + ' &nbsp;&nbsp;' + data[i].payService + '元/' + data[i].unit + '&nbsp;&nbsp;' + data[i].payServiceNum + data[i].unit + '&nbsp;&nbsp;医生:' + data[i].doctor +
                    ' <a href="javascript:;" style="color: #2fd255" onclick="openPayService(\'' + data[i].id + '\')">&nbsp修改</a> <a href="javascript:;" style="color: #db6693" onclick=\"deletePayServices(\'' + data[i].id + '\')\">&nbsp;&nbsp;删除</a>' + '&nbsp;&nbsp;折后价：<span  class="totalzhe" id="zhe_' + data[i].id + '" style="color: #2eadd0;font-size: 2em;font-weight: bold;">' + foldingPay.toFixed(2) + ' </span>元'
                    + '<a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney1(\'' + data[i].id + '\')">&nbsp修改折后价</a>&nbsp&nbsp;<span>实收：</span><input class="serviceId" type="hidden" value="' + data[i].payServiceId + '"/><input class="serviceIds" type="hidden" value="' + data[i].id + '"/><input type="text" class="totleinput" style="border: 1px solid black;width: 115px;border-radius: 5px;padding-left: 5px;" value="' + foldingPay.toFixed(2) + '" oninput="inputNum()"/>' + '' +
                    '</div>';
            }
            txt += ' <div><div style="position: absolute;right: 3%;bottom: 8%;"><p>折后金额：<span style="font-weight: bold;font-size: 22px;">' + totleFoldingPay.toFixed(2) + '</span>元 ' +
                //     ' <a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney(\'' + customerId + '\')">&nbsp修改折后价</a></p>' +
                '<p id="arrearsOfMoney">实收金额：<input id="serviceActualPay" style="color: #30acd0;font-weight: bold;font-size: 22px;border: none;background: #f7f7f7;width:100px;" readonly="readonly"  value="' + totleFoldingPay.toFixed(2) + '"/>元 </p></div></div>';
            txt += '</ul>';
            window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
            window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
        }
        Feng.success("修改成功!");
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};

PayServiceInfoDlg.updateMoney1 = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var updateMoney = $('#updateMoney').val();
    var zheMoney = $("#zheMoney").val();
    var lowestMoney = $("#lowestMoney").val();
    var highestMoney = $("#highestMoney").val();
    if (updateMoney != "" && updateMoney == "") {
        alert("请输入修改金额！");
        return
    }
    if (parseFloat(updateMoney) > parseFloat(highestMoney)) {
        alert("修改金额不能大于最高金额，请重新输入！...");
        return
    }
    if (parseFloat(updateMoney) < parseFloat(lowestMoney)) {
        alert("修改金额不能小于最低金额，请重新输入！...");
        return
    }
    var ajax = new $ax(Feng.ctxPath + "/payService/updateMoney1", function (data) {
        Feng.success("修改成功!");
        var txt = '<ul>';
        var totleReceivable = 0.00;
        var totleFoldingPay = 0.00;
        var customerId = window.parent.document.getElementById("customerId").value;
        if (data.length == 0) {
            window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
            window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
        } else {
            for (var i = 0; i < data.length; i++) {
                var pnum = data[i].payServiceNum;
                var psptwo = data[i].payServicePriceTwo;
                var foldingPay = data[i].foldingPay;
                totleReceivable += pnum * psptwo.toFixed(2);
                totleFoldingPay += foldingPay;
                txt += ' <div class="data_row" style="margin-top: 5px">' +
                    '                <div style=\"padding-left: 10px;width: 120px;display: inline-block;position: relative;top: 11px;\">\n' +
                    '                    <div style=\"border-bottom: 1px solid;\">\n' +
                    '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_top\">' + data[i].leftUp + '</div><!--\n' +
                    '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightUp + '</div>\n' +
                    '                   </div>\n' +
                    '                    <div>\n' +
                    '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_bottom\">' + data[i].leftDown + '</div><!--\n' +
                    '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightDown + '</div>\n' +
                    '                   </div>\n' +
                    '                </div>\n' +
                    ' ' + data[i].department + '&nbsp;&nbsp;' + data[i].firstLevelProject + '&nbsp;&nbsp;' + data[i].payServiceName + ' &nbsp;&nbsp;' + data[i].payService + '元/' + data[i].unit + '&nbsp;&nbsp;' + data[i].payServiceNum + data[i].unit + '&nbsp;&nbsp;医生:' + data[i].doctor +
                    ' <a href="javascript:;" style="color: #2fd255" onclick="openPayService(\'' + data[i].id + '\')">&nbsp修改</a> <a href="javascript:;" style="color: #db6693" onclick=\"deletePayServices(\'' + data[i].id + '\')\">&nbsp;&nbsp;删除</a>' + '&nbsp;&nbsp;折后价：<span  class="totalzhe" id="zhe_' + data[i].id + '" style="color: #2eadd0;font-size: 2em;font-weight: bold;">' + foldingPay.toFixed(2) + ' </span>元'
                    + '<a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney1(\'' + data[i].id + '\')">&nbsp修改折后价</a>&nbsp&nbsp;<span>实收：</span><input class="serviceId" type="hidden" value="' + data[i].payServiceId + '"/><input class="serviceIds" type="hidden" value="' + data[i].id + '"/><input type="text" class="totleinput" style="border: 1px solid black;width: 115px;border-radius: 5px;padding-left: 5px;" value="' + foldingPay.toFixed(2) + '" oninput="inputNum()"/>' + '' +
                    '</div>';
            }
            txt += ' <div><div style="position: absolute;right: 3%;bottom: 8%;"><p>折后金额：<span style="font-weight: bold;font-size: 22px;">' + totleFoldingPay.toFixed(2) + '</span>元 ' + //' <a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney(\'' + customerId + '\')">&nbsp修改折后价</a></p>' +
                '<p id="arrearsOfMoney">实收金额：<input id="serviceActualPay" style="color: #30acd0;font-weight: bold;font-size: 22px;border: none;background: #f7f7f7;width:100px;" readonly="readonly"  value="' + totleFoldingPay.toFixed(2) + '"/>元 </p></div></div>';
            txt += '</ul>';
            window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
            window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
        }
        Feng.success("修改成功!");
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};

PayServiceInfoDlg.updateMoney2 = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var updateMoney = $('#updateMoney').val();
    var zheMoney = $("#zheMoney").val();
    var lowestMoney = $("#lowestMoney").val();
    var highestMoney = $("#highestMoney").val();
    if (updateMoney != "" && updateMoney == "") {
        alert("请输入修改金额！");
        return
    }
    if (parseFloat(updateMoney) > parseFloat(highestMoney)) {
        alert("修改金额不能大于最高金额，请重新输入！...");
        return
    }
    var ajax = new $ax(Feng.ctxPath + "/payService/updateMoney1", function (data) {
        Feng.success("修改成功!");
        var txt = '<ul>';
        var totleReceivable = 0.00;
        var totleFoldingPay = 0.00;
        var customerId = window.parent.document.getElementById("customerId").value;
        if (data.length == 0) {
            window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
            window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
        } else {
            for (var i = 0; i < data.length; i++) {
                var pnum = data[i].payServiceNum;
                var psptwo = data[i].payServicePriceTwo;
                var foldingPay = data[i].foldingPay;
                totleReceivable += pnum * psptwo.toFixed(2);
                totleFoldingPay += foldingPay;
                txt += ' <div class="data_row" style="margin-top: 5px">' +
                    '                <div style=\"padding-left: 10px;width: 120px;display: inline-block;position: relative;top: 11px;\">\n' +
                    '                    <div style=\"border-bottom: 1px solid;\">\n' +
                    '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_top\">' + data[i].leftUp + '</div><!--\n' +
                    '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightUp + '</div>\n' +
                    '                   </div>\n' +
                    '                    <div>\n' +
                    '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_bottom\">' + data[i].leftDown + '</div><!--\n' +
                    '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightDown + '</div>\n' +
                    '                   </div>\n' +
                    '                </div>\n' +
                    ' ' + data[i].department + '&nbsp;&nbsp;' + data[i].firstLevelProject + '&nbsp;&nbsp;' + data[i].payServiceName + ' &nbsp;&nbsp;' + data[i].payService + '元/' + data[i].unit + '&nbsp;&nbsp;' + data[i].payServiceNum + data[i].unit + '&nbsp;&nbsp;医生:' + data[i].doctor +
                    ' <a href="javascript:;" style="color: #2fd255" onclick="openPayService(\'' + data[i].id + '\')">&nbsp修改</a> <a href="javascript:;" style="color: #db6693" onclick=\"deletePayServices(\'' + data[i].id + '\')\">&nbsp;&nbsp;删除</a>' + '&nbsp;&nbsp;折后价：<span  class="totalzhe" id="zhe_' + data[i].id + '" style="color: #2eadd0;font-size: 2em;font-weight: bold;">' + foldingPay.toFixed(2) + ' </span>元'
                    + '<a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney1(\'' + data[i].id + '\')">&nbsp修改折后价</a>&nbsp&nbsp;<span>实收：</span><input class="serviceId" type="hidden" value="' + data[i].payServiceId + '"/><input class="serviceIds" type="hidden" value="' + data[i].id + '"/><input type="text" class="totleinput" style="border: 1px solid black;width: 115px;border-radius: 5px;padding-left: 5px;" value="' + foldingPay.toFixed(2) + '" oninput="inputNum()"/>' + '' +
                    '</div>';
            }
            txt += ' <div><div style="position: absolute;right: 3%;bottom: 8%;"><p>折后金额：<span style="font-weight: bold;font-size: 22px;">' + totleFoldingPay.toFixed(2) + '</span>元 ' + //' <a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney(\'' + customerId + '\')">&nbsp修改折后价</a></p>' +
                '<p id="arrearsOfMoney">实收金额：<input id="serviceActualPay" style="color: #30acd0;font-weight: bold;font-size: 22px;border: none;background: #f7f7f7;width:100px;" readonly="readonly"  value="' + totleFoldingPay.toFixed(2) + '"/>元 </p></div></div>';
            txt += '</ul>';
            window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
            window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
        }
        Feng.success("修改成功!");
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};

PayServiceInfoDlg.distributiveDoctor = function () {
    var customerId = document.getElementById("customerId").value;
    var index = layer.open({
        type: 2,
        title: '分配医生',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_distributiveDoctor/' + customerId
    });
    this.layerIndex = index;
};


PayServiceInfoDlg.distributiveNurse = function () {
    var customerId = document.getElementById("customerId").value;
    var index = layer.open({
        type: 2,
        title: '分配医助/护士',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_distributiveNurse/' + customerId
    });
    this.layerIndex = index;
};

PayServiceInfoDlg.distributiveReferrer = function () {
    var customerId = document.getElementById("customerId").value;
    var index = layer.open({
        type: 2,
        title: '添加转诊人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_distributiveReferrer/' + customerId
    });
    this.layerIndex = index;
};




var getCount3 = 0;
/**
 * 提交添加
 */
PayServiceInfoDlg.paySubmitss = function () {
    getCount3++;
    if (getCount3 == 1) {
        this.clearData();
        this.collectData();
        if (!this.validate()) {
            return;
        }
        var payServiceNum = document.getElementById("payServiceNum").value;
        var payServicePriceOne = document.getElementById("payServicePriceOne").value;
        var payServicePriceTwo = document.getElementById("payServicePriceTwo").value;
        var totalAmount = document.getElementById("totalAmount").value;
        var doctor = document.getElementById("doctor").value;
        if (payServicePriceOne * payServiceNum > totalAmount) {
            alert("总金额不能小于价格区间应收最小金额");
            return;
        }
        if (payServicePriceTwo * payServiceNum < totalAmount) {
            alert("总金额不能大于价格区间应收最大金额");
            return;
        }
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/payService/addPay", function (data) {
            var txt = '<ul>';
            var totleReceivable = 0.00;
            var totleFoldingPay = 0.00;
            var customerId = document.getElementById("customerId").value;
            if (data.length == 0) {
                window.parent.parent.document.getElementById('detailedListDiv').innerHTML = txt;
                window.parent.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
            } else {
                for (var i = 0; i < data.length; i++) {
                    console.log(data);
                    var pnum = data[i].payServiceNum;
                    var psptwo = data[i].payServicePriceTwo;
                    var foldingPay = data[i].foldingPay;
                    totleReceivable += pnum * psptwo.toFixed(2);
                    totleFoldingPay += foldingPay;
                    txt += ' <div class="data_row" style="margin-top: 5px">' +
                        '                <div style=\"padding-left: 10px;width: 120px;display: inline-block;position: relative;top: 11px;\">\n' +
                        '                    <div style=\"border-bottom: 1px solid;\">\n' +
                        '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_top\">' + data[i].leftUp + '</div><!--\n' +
                        '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightUp + '</div>\n' +
                        '                   </div>\n' +
                        '                    <div>\n' +
                        '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_bottom\">' + data[i].leftDown + '</div><!--\n' +
                        '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightDown + '</div>\n' +
                        '                   </div>\n' +
                        '                </div>\n' +
                        ' ' + data[i].department + '&nbsp;&nbsp;' + data[i].firstLevelProject + '&nbsp;&nbsp;' + data[i].payServiceName + ' &nbsp;&nbsp;' + data[i].payService + '元/' + data[i].unit + '&nbsp;&nbsp;' + data[i].payServiceNum + data[i].unit + '&nbsp;&nbsp;医生:' + data[i].doctor + '&nbsp;&nbsp;医助/护士:' + data[i].nurse +
                        ' <a href=\"javascript:;\" style="color: #2fd255" onclick=\"openPayService(\'' + data[i].id + '\')\">&nbsp修改</a> <a href=\"javascript:;\" style="color: #db6693" onclick=\"deletePayServices(\'' + data[i].id + '\')\">&nbsp;&nbsp;删除</a>' + '&nbsp;&nbsp;折后价：<span id="zhe_' + data[i].id + '" class="totalzhe" style="color: #2eadd0;font-size: 2em;font-weight: bold;">' + foldingPay.toFixed(2) + ' </span>元'
                        + '<a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney1(\'' + data[i].id + '\')">&nbsp修改折后价</a>&nbsp&nbsp;<span>实收：</span><input class="serviceId" type="hidden" value="' + data[i].payServiceId + '"/><input class="serviceIds" type="hidden" value="' + data[i].id + '"/><input type="text" class="totleinput" style="border: 1px solid black;width: 115px;border-radius: 5px;padding-left: 5px;" value="' + foldingPay.toFixed(2) + '" oninput="inputNum()"/>' + '' +
                        '</div>';
                }
                txt += '<div><div style="position: absolute;right: 3%;bottom: 8%;"><p>折后金额：<span style="font-weight: bold;font-size: 22px;">' + totleFoldingPay.toFixed(2) + '</span>元 ' + //' <a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney(\'' + customerId + '\')">&nbsp修改折后价</a></p>' +
                    '<p id="arrearsOfMoney">实收金额：<input id="serviceActualPay" style="color: #30acd0;font-weight: bold;font-size: 22px;border: none;background: #f7f7f7;width:100px;" readonly="readonly" value="' + totleFoldingPay.toFixed(2) + '" />元</p></div></div> ';
                txt += '</ul>';
                window.parent.parent.document.getElementById('detailedListDiv').innerHTML = txt;
                window.parent.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
            }
            Feng.success("添加成功!");
            $('#onOpenBill', parent.document).click();
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.payServiceInfoData);
        ajax.start();
    }
};

/**
 * 分配医生
 */
PayServiceInfoDlg.distributiveDoctor = function () {
    var customerId = document.getElementById("customerId").value;
    var index = layer.open({
        type: 2,
        title: '分配医生',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_distributiveDoctor/' + customerId
    });
    this.layerIndex = index;
};

var getCount = 0;
/**
 * 提交添加
 */
PayServiceInfoDlg.paySubmit = function () {
    var payServiceNum = document.getElementById("payServiceNum").value;
    var payServicePriceOne = document.getElementById("payServicePriceOne").value;
    var payServicePriceTwo = document.getElementById("payServicePriceTwo").value;
    var totalAmount = document.getElementById("totalAmount").value;
    var doctor = document.getElementById("doctor").value;
    var nurse = document.getElementById("nurse").value;
    var referraler = document.getElementById("referraler").value;
    console.log(referraler);
    if (payServicePriceOne * payServiceNum > totalAmount) {
        alert("总金额不能小于价格区间应收最小金额");
        return;
    }
    if (payServicePriceTwo * payServiceNum < totalAmount) {
        alert("总金额不能大于价格区间应收最大金额");
        return;
    }
    if(payServiceNum==0 || payServiceNum<0){
        alert("数量必须大于0");
        return;
    }
    if(doctor==null || doctor==""){
        alert("请选择医生!");
        return;
    }

    getCount++;
    if (getCount == 1) {
        this.clearData();
        this.collectData();
        if (!this.validate()) {
            return;
        }
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/payService/addPay", function (data) {
            console.log(data);
            var txt = '<ul>';
            var totleReceivable = 0.00;
            var totleFoldingPay = 0.00;
            var customerId = document.getElementById("customerId").value;
            if (data.length == 0) {
                window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
                window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
            } else {
                for (var i = 0; i < data.length; i++) {
                    var pnum = data[i].payServiceNum;
                    var psptwo = data[i].payServicePriceTwo;
                    var foldingPay = data[i].foldingPay;
                    totleReceivable += pnum * psptwo.toFixed(2);
                    totleFoldingPay += foldingPay;
                    txt += ' <div class="data_row" style="margin-top: 5px">' +
                        '                <div style=\"padding-left: 10px;width: 120px;display: inline-block;position: relative;top: 11px;\">\n' +
                        '                    <div style=\"border-bottom: 1px solid;\">\n' +
                        '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_top\">' + data[i].leftUp + '</div><!--\n' +
                        '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightUp + '</div>\n' +
                        '                   </div>\n' +
                        '                    <div>\n' +
                        '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_bottom\">' + data[i].leftDown + '</div><!--\n' +
                        '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightDown + '</div>\n' +
                        '                   </div>\n' +
                        '                </div>\n' +
                        '' + data[i].department + '&nbsp;&nbsp;' + data[i].firstLevelProject + '&nbsp;&nbsp;' + data[i].payServiceName + '&nbsp;&nbsp; ' + data[i].payService + '元/' + data[i].unit + '&nbsp;&nbsp;' + data[i].payServiceNum + data[i].unit + '&nbsp;&nbsp;医生:' + data[i].doctor + '&nbsp;&nbsp;医助/护士:' + data[i].nurse +
                        ' &nbsp;<a href=\"javascript:;\" style="color: #2fd255" onclick=\"openPayService(\'' + data[i].id + '\')\">&nbsp修改</a> <a href=\"javascript:;\" style="color: #db6693" onclick=\"deletePayServices(\'' + data[i].id + '\')\">&nbsp;删除</a>' + '&nbsp;&nbsp;折后价：<span id="zhe_' + data[i].id + '" class="totalzhe" style="color: #2eadd0;font-size: 2em;font-weight: bold;">' + foldingPay.toFixed(2) + ' </span>元'
                        + '<a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney1(\'' + data[i].id + '\')">&nbsp修改折后价</a>&nbsp&nbsp;<span>实收：</span><input class="serviceId" type="hidden" value="' + data[i].payServiceId + '"/><input class="serviceIds" type="hidden" value="' + data[i].id + '"/><input type="text" style="border: 1px solid black;width: 115px;border-radius: 5px;padding-left: 5px;" class="totleinput" value="' + foldingPay.toFixed(2) + '" oninput="inputNum()"/>' + '' +
                        '</div>';
                }
                txt += ' <div><div style="position: absolute;right: 3%;bottom: 8%;"><p>折后金额：<span style="font-weight: bold;font-size: 22px;">' + totleFoldingPay.toFixed(2) + '</span>元 ' + //' <a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney(\'' + customerId + '\')">&nbsp修改折后价</a></p>' +
                    '<p id="arrearsOfMoney">实收金额：<input id="serviceActualPay" style="color: #30acd0;font-weight: bold;font-size: 22px;border: none;background: #f7f7f7;width:100px;" readonly="readonly" value="' + totleFoldingPay.toFixed(2) + '"/> 元</p></div></div>';
                txt += '</ul>';
                window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
                window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
            }
            Feng.success("添加成功!");
            PayServiceInfoDlg.close();
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.payServiceInfoData);
        ajax.start();
    }
};

var getCount2 = 0;
/**
 * 修改 提交
 */
PayServiceInfoDlg.addPayUpdate = function () {
    var payServiceNum = document.getElementById("payServiceNum").value;
    var payServicePriceOne = document.getElementById("payServicePriceOne").value;
    var payServicePriceTwo = document.getElementById("payServicePriceTwo").value;
    var totalAmount = document.getElementById("totalAmount").value;
    var doctor = document.getElementById("doctor").value;
    var nurse = document.getElementById("nurse").value;
    if (payServicePriceOne * payServiceNum > totalAmount) {
        alert("总金额不能小于价格区间应收最小金额");
        return;
    }
    if (payServicePriceTwo * payServiceNum < totalAmount) {
        alert("总金额不能大于价格区间应收最大金额");
        return;
    }
    if(payServiceNum==0 || payServiceNum<0){
        alert("数量必须大于0");
        return;
    }
    if(doctor==null || doctor==""){
        alert("请选择医生!");
        return;
    }

    getCount2++;
    if (getCount2 == 1) {
        this.clearData();
        this.collectData();
        if (!this.validate()) {
            return;
        }

        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/payService/addPayUpdate", function (data) {
            var txt = '<ul>';
            var totleReceivable = 0.00;
            var totleFoldingPay = 0.00;
            var customerId = document.getElementById("customerId").value;
            if (data.length == 0) {
                window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
                window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;
            } else {
                for (var i = 0; i < data.length; i++) {
                    console.log(data);
                    var pnum = data[i].payServiceNum;
                    var psptwo = data[i].payServicePriceTwo;
                    var foldingPay = data[i].foldingPay;
                    totleReceivable += pnum * psptwo.toFixed(2);
                    totleFoldingPay += foldingPay;
                    txt += ' <div class="data_row" style="margin-top: 5px">' +
                        '                <div style=\"padding-left: 10px;width: 120px;display: inline-block;position: relative;top: 11px;\">\n' +
                        '                    <div style=\"border-bottom: 1px solid;\">\n' +
                        '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_top\">' + data[i].leftUp + '</div><!--\n' +
                        '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightUp + '</div>\n' +
                        '                   </div>\n' +
                        '                    <div>\n' +
                        '                        <div style=\"font-size: 12px;display: inline-block;width:55px;border-right: 1px solid black;text-align: right;height: 22px;margin-bottom: -5px;\" id=\"yw_left_bottom\">' + data[i].leftDown + '</div><!--\n' +
                        '                         --><div style=\"font-size: 12px;display: inline-block;width:55px;\" id=\"yw_right_top\">' + data[i].rightDown + '</div>\n' +
                        '                   </div>\n' +
                        '                </div>\n' +
                        '' + data[i].department + '&nbsp;&nbsp;' + data[i].firstLevelProject + '&nbsp;&nbsp;' + data[i].payServiceName + '&nbsp;&nbsp; ' + data[i].payService + '元/' + data[i].unit + '&nbsp;&nbsp;' + data[i].payServiceNum + data[i].unit + '&nbsp;&nbsp;医生:' + data[i].doctor + '&nbsp;&nbsp;医助/护士:' + data[i].nurse +
                        ' <a href=\"javascript:;\" style="color: #2fd255" onclick=\"openPayService(\'' + data[i].id + '\')\">&nbsp修改</a> <a href=\"javascript:;\" style="color: #db6693" onclick=\"deletePayServices(\'' + data[i].id + '\')\">&nbsp;&nbsp;删除</a>' + '&nbsp;&nbsp;折后价：<span id="zhe_' + data[i].id + '"class="totalzhe"  style="color: #2eadd0;">' + foldingPay.toFixed(2) + ' </span>元'
                        + '<a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney1(\'' + data[i].id + '\')">&nbsp修改折后价</a>&nbsp&nbsp;<span>实收：</span><input class="serviceId" type="hidden" value="' + data[i].payServiceId + '"/><input class="serviceIds" type="hidden" value="' + data[i].id + '"/><input type="text" style="border: 1px solid black;width: 115px;border-radius: 5px;padding-left: 5px;" class="totleinput" value="' + foldingPay.toFixed(2) + '" oninput="inputNum()"/>' + '' +
                        '</div>';
                    '</div>';
                }
                txt += '<div><div style="position: absolute;right: 3%;bottom: 8%;"><p>折后金额：<span style="font-weight: bold;font-size: 22px;">' + totleFoldingPay.toFixed(2) + '</span>元 ' + //' <a href="javascript:;" style="color: #31d115;" onclick="openUpdateMoney(\'' + customerId + '\')">&nbsp修改折后价</a></p>' +
                    '<p id="arrearsOfMoney">实收金额：<input id="serviceActualPay" style="color: #30acd0;font-weight: bold;font-size: 22px;border: none;background:#f7f7f7;width:100px;" readonly="readonly" value="' + totleFoldingPay.toFixed(2) + '"/>元 </p></div></div> ';
                txt += '</ul>';
                window.parent.document.getElementById('detailedListDiv').innerHTML = txt;
                window.parent.document.getElementById('totleFoldingPays').value = totleFoldingPay;

            }
            Feng.success("添加成功!");
            PayServiceInfoDlg.close();
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.payServiceInfoData);
        ajax.start();
    }
};

/**
 * 提交修改
 */
PayServiceInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var payServicePriceOne = $('#payServicePriceOne').val();
    var payServicePriceTwo = $('#payServicePriceTwo').val();
    if (payServicePriceOne != "" && payServicePriceOne != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceOne))) {
            alert("产品价格1整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    if (payServicePriceTwo != "" && payServicePriceTwo != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceTwo))) {
            alert("产品价格2整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/payService/updatePayService", function (data) {
        Feng.success("修改成功!");
        window.parent.PayService.table.refresh();
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentPayServiceMenu" || $(
            event.target).parents("#parentPayServiceMenu").length > 0)) {
        PayServiceInfoDlg.hidePayServiceSelectTree();
    }
}

$(function () {
    Feng.initValidator("payServiceInfoForm", PayServiceInfoDlg.validateFields);
});
